import bagel.Image;
import bagel.Input;
import bagel.Keys;
import bagel.util.Colour;
import bagel.util.Point;
import bagel.util.Rectangle;
import bagel.util.Vector2;

import java.util.Vector;

import bagel.Drawing;

public class Mario {
    private Point position;
    private Image imageLeft; // 马里奥面向左的图像
    private Image imageRight; // 马里奥面向右的图像
    private Image marioHammerLeft;
    private Image marioHammerRight;//马里奥拿着锤子向右的图像
    
    private Image currentImage; // 当前渲染的图像
    private static final double MOVE_SPEED = 3.5; // 马里奥的移动速度
    private static final double CLIMB_SPEED = 2; // 爬梯子速度
    private double verticalSpeed = 0; // 马里奥的垂直速度
    private static final double MAX_FALLING_SPEED = 10; // 最大掉落速度
    private static final double JUMP_SPEED = -5; // 跳跃初速度
    private static final int JUMP_OVER_BARREL_POINT = 30;//跳过木桶的分数
    private static final int DESTROY_BARREL_POINT = 100;//摧毁木桶的分数
    
    private CollisionManager collisionManager;
    private boolean onPlatform = false; // 跟踪马里奥是否在平台上
    private boolean onLadder = false; // 跟踪马里奥是否在梯子上
    private double width,height;
    private boolean alive = true;
    private int barrelLeft=0,barrelRight=0;
    private int score = 0;
    private boolean withHammer = false;
    private boolean win = false;

    public Mario(Point position, Image imageLeft, Image imageRight, Image marioHammerLeft, Image marioHammerRight,CollisionManager collisionManager) {
        this.position = position;
        this.imageLeft = imageLeft;
        this.imageRight = imageRight;
        this.marioHammerLeft = marioHammerLeft;
        this.marioHammerRight=marioHammerRight;

        this.currentImage = imageRight; // 默认面向右
        this.collisionManager = collisionManager;
        this.width = imageLeft.getWidth();
        this.height = imageLeft.getHeight();
    }

    public void update(Input input) {
        // 检测键盘输入并更新位置
        if (input.isDown(Keys.LEFT)) {
            position = new Point(position.x - MOVE_SPEED, position.y);
            currentImage = withHammer?marioHammerLeft:imageLeft; // 更新当前图像为面向左
        }

        if (input.isDown(Keys.RIGHT)) {
            position = new Point(position.x + MOVE_SPEED, position.y);
            currentImage = withHammer?marioHammerRight:imageRight; // 更新当前图像为面向右
        }

        //防止马里奥超过屏幕范围
        if(position.x - width/2 < 0){
            position = new Point( width/2,position.y);
        }
        if(position.x + width/2 > Common.SCREEN_WIDTH){
            position = new Point(Common.SCREEN_WIDTH - width/2,position.y);
        }

        if(input.isDown(Keys.UP) && onLadder){
            position = new Point(position.x,position.y-CLIMB_SPEED);
        }

        if(input.isDown(Keys.DOWN) && onLadder ){
            position = new Point(position.x,position.y+CLIMB_SPEED);
        }


        // 检测跳跃输入
        if (input.isDown(Keys.SPACE) && onPlatform) {
            verticalSpeed = JUMP_SPEED; // 设置跳跃速度
        }

        if(onPlatform){
            Point barrelCount = collisionManager.getBarrelCount(position,width,height);
            int left=(int)barrelCount.x;
            int right=(int)barrelCount.y;
            if(left+right == barrelLeft+barrelRight && left!=barrelLeft){
                score += JUMP_OVER_BARREL_POINT;
            }
            barrelLeft=left;
            barrelRight=right;
        }
       

        if(collisionManager.OnLadder(position, width, height)){
            onLadder = true;
        }else{
            onLadder = false;
        }

        //如果在梯子上，直接结束，不考虑重力
        if(onLadder){
            verticalSpeed = 0;
            return;
        }

        // 处理平台碰撞
        Point tmp = collisionManager.OnPlatform(position,width,height);
        if (null != tmp) {
            position = tmp;
            verticalSpeed = 0; // 设置垂直速度为 0
            onPlatform = true;
        } else {
            // 如果没有碰撞，应用重力
            verticalSpeed += Common.GRAVITY;
            if (verticalSpeed > MAX_FALLING_SPEED) {
                verticalSpeed = MAX_FALLING_SPEED; // 限制最大掉落速度
            }
            position = new Point(position.x, position.y + verticalSpeed);  
            onPlatform = false;
        }

        Enemy barrel = collisionManager.OnBarrels(position,width,height);
        if(null != barrel){
            if(withHammer){
                score+=DESTROY_BARREL_POINT;
                collisionManager.removeBarrel(barrel);
            }else{
                alive = false;
            }
           
        }

        if(collisionManager.OnKong(position,width,height)){
            if(withHammer){
                win = true;
            }else{
                alive = false;
            }
        }
        if(collisionManager.OnHammer(position,width,height)){
            withHammer = true;
            width = marioHammerLeft.getWidth();
            height=marioHammerRight.getHeight();
        }
        if(withHammer){
            if(currentImage == imageLeft){
                currentImage = marioHammerLeft;
            }else if(currentImage != marioHammerLeft){
                currentImage = marioHammerRight;
            }

        }
    }

    public boolean hasHammer(){
        return withHammer;
    }

    public boolean isAlive(){
        return alive;
    }

    public boolean getWin(){
        return win;
    }

    public int getScore(){
        return score;
    }

    public Point getPosition(){
        return position;
    }
    public void render() {
        currentImage.draw(position.x, position.y);
        /*
        // 计算碰撞箱的四个角的坐标
        double left = position.x - width / 2;
        double right = position.x + width / 2;
        double top = position.y - height / 2;
        double bottom = position.y + height / 2;
        Point lt = new Point(left,top);
        Point rt = new Point(right,top);
        Point lb = new Point(left,bottom);
        Point rb = new Point(right,bottom);

        // 绘制矩形的四条边
        Drawing.drawLine(lt,rt,1, Colour.RED); // 上边
        Drawing.drawLine(rt,rb, 1,Colour.RED); // 右边
        Drawing.drawLine(rb,lb, 1,Colour.RED); // 下边
        Drawing.drawLine(lb,lt, 1,Colour.RED); // 左边
         */
    }
}